package com.vincent.suchao.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.vincent.suchao.entity.CGAL_DetailFeed;
import com.vincent.suchao.entity.CGAL_Feed;
import com.vincent.suchao.entity.ZXDT_NewsFeed;
import com.vincent.suchao.entity.ZXDT_NewsFeed_Detail;
import com.vincent.suchao.lcld.CGAL_DetailActivity;
import com.vincent.suchao.lcld.R;

public class Util {
	public static final String NEWS_ZXDT_URL = "http://app.cdlcld.com/mob.php?m=mobile&c=index&a=lists";

	public static final String NEWS_URL2 = "http://app.cdlcld.com/mob.php?m=mobile&c=index&a=show";

	public static final int TIMEOUT_POST_CONNECTION = 10000;

	// {
	// "t":"1364891980",
	// "v":"d04072c0b4c5d889f58509f8c6c515e2",
	// "catid":19,
	// "_lk":"id,title,thumb,description,updatetime",
	// "page":1,
	// "pagesize":10,
	// "order":"v"
	// }

	public static Dialog progressDialog(Context context, int resIdTitle, int resIdMsg, boolean cancelable) {
		LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflator.inflate(R.layout.inflate_progress_bar, null);
		TextView textView = (TextView) view.findViewById(android.R.id.text1);
		AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(view).setCancelable(cancelable);
		// builder.setTitle(context.getString(resIdTitle));
		textView.setText(context.getString(resIdMsg));
		builder.setCancelable(true);
		return builder.create();
	}

	public static String testZXDT(int page) {

		JSONObject object = new JSONObject();
		try {
			object.put("t", "1364891980");
			object.put("v", "d04072c0b4c5d889f58509f8c6c515e2");
			object.put("catid", 6);
			object.put("_lk", "id,title,thumb,description,updatetime");
			object.put("page", page);
			object.put("pagesize", 10);
			object.put("order", "v");
		} catch (JSONException e) {

			e.printStackTrace();

		}

		return object.toString();
	}

	// {"catid":8,
	// "id":18,
	// "_ck":"*",
	// "t":"1364891980",
	// "v":"d04072c0b4c5d889f58509f8c6c515e2"
	// }

	public static String testCGAL() {

		JSONObject object = new JSONObject();
		try {
			object.put("t", "1364891980");
			object.put("v", "d04072c0b4c5d889f58509f8c6c515e2");
			object.put("catid", 8);
			object.put("id", 18);
			object.put("_ck", "*");
		} catch (JSONException e) {

			e.printStackTrace();

		}

		return object.toString();
	}

	public static String testCGAL_Detail(int id) {

		JSONObject object = new JSONObject();
		try {
			object.put("t", "1364891980");
			object.put("v", "d04072c0b4c5d889f58509f8c6c515e2");
			object.put("catid", 8);
			object.put("id", id);
			object.put("_ck", "*");
		} catch (JSONException e) {

			e.printStackTrace();

		}

		return object.toString();
	}

	public static String post(String urlPath, String data) throws Exception {

		URL url = new URL(urlPath);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setRequestMethod("POST");
		conn.setUseCaches(false);
		conn.setInstanceFollowRedirects(true);
		conn.setConnectTimeout(TIMEOUT_POST_CONNECTION);
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		// conn.setRequestProperty("User-Agent",
		// System.getProperties().getProperty("http.agent"));
		conn.setRequestProperty("Connection", "Keep-Alive");
		conn.setRequestProperty("Charset", "UTF-8");
		// conn.connect();
		DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
		dos.writeBytes(data);
		dos.flush();
		String response = read(conn.getInputStream());
		dos.close();
		return response;
	}

	public static String read(InputStream in) throws IOException {
		StringBuilder sb = new StringBuilder();
		BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
		for (String line = r.readLine(); line != null; line = r.readLine()) {
			sb.append(line);
		}
		in.close();
		return sb.toString();
	}

	public static ArrayList<ZXDT_NewsFeed> parseStoresJSON(String result) throws JSONException {
		JSONObject data = new JSONObject(result).getJSONObject("data");
		JSONArray datasJsonArray = data.getJSONArray("lists");
		ArrayList<ZXDT_NewsFeed> feeds = new ArrayList<ZXDT_NewsFeed>();

		for (int i = 0; i < datasJsonArray.length(); i++) {
			JSONObject jsonObject = (JSONObject) datasJsonArray.opt(i);

			int id = jsonObject.getInt("id");
			String description = jsonObject.getString("description");
			String title = jsonObject.getString("title");
			String thumb = jsonObject.getString("thumb");
			String url = jsonObject.getString("url");
			String updatetime = jsonObject.getString("updatetime");
			ZXDT_NewsFeed f = new ZXDT_NewsFeed();
			f.setId(id);
			f.setDescription(description);
			f.setTitle(title);
			f.setThumb(thumb);
			f.setUpdatetime(updatetime);
			f.setUrl(url);
			feeds.add(f);
		}
		return feeds;
	}

	public static ZXDT_NewsFeed_Detail parseZXDTJSONDetail(String result) throws JSONException {
		JSONObject data = new JSONObject(result).getJSONObject("data");
		String title = data.getString("title");
		String content = data.getString("content");
		String inputtime = data.getString("inputtime");
		ZXDT_NewsFeed_Detail z = new ZXDT_NewsFeed_Detail(title, content, inputtime);
		return z;
	}

	public static ArrayList<CGAL_Feed> parseCGALJSON(String result) throws JSONException {
		JSONObject data = new JSONObject(result).getJSONObject("data");
		JSONArray datasJsonArray = data.getJSONArray("lists");
		ArrayList<CGAL_Feed> feeds = new ArrayList<CGAL_Feed>();

		for (int i = 0; i < datasJsonArray.length(); i++) {
			JSONObject jsonObject = (JSONObject) datasJsonArray.opt(i);

			int id = jsonObject.getInt("id");
			String description = jsonObject.getString("description");
			String title = jsonObject.getString("title");
			String thumb = jsonObject.getString("thumb");
			String url = jsonObject.getString("url");
			String updatetime = jsonObject.getString("updatetime");
			CGAL_Feed f = new CGAL_Feed();
			f.setId(id);
			f.setDescription(description);
			f.setTitle(title);
			f.setThumb(thumb);
			f.setUpdatetime(updatetime);
			f.setUrl(url);
			feeds.add(f);
		}
		return feeds;
	}

	public static CGAL_DetailFeed parseCGALJSONDetail(String result) throws JSONException {
		JSONObject data = new JSONObject(result).getJSONObject("data");
		JSONArray datasJsonArray = data.getJSONArray("images");
		CGAL_DetailFeed f = new CGAL_DetailFeed();
		String description = data.getString("description");
		String title = data.getString("title");
		String updatetime = data.getString("updatetime");
		String content = data.getString("content");
		f.setDescription(description);
		f.setTitle(title);
		f.setUpdatetime(updatetime);
		f.setContent(content);
		String[] imagesUrl = new String[datasJsonArray.length()];
		String[] alts = new String[datasJsonArray.length()];
		for (int i = 0; i < datasJsonArray.length(); i++) {
			JSONObject jsonObject = (JSONObject) datasJsonArray.opt(i);
			String url = jsonObject.getString("url");
			String alt = jsonObject.getString("alt");
			imagesUrl[i] = url;
			alts[i] = alt;
		}
		f.setImagesUrl(imagesUrl);
		f.setAlt(alts);
		return f;
	}
}
